package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {
    @DubboReference
    private RoleService roleService;

    @GetMapping("/findByPage")
    public Result findByPage(QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(queryPageBean);
        log.info("[IdentityController] pageResult:{}",pageResult);
        if (pageResult!=null){
            return new Result(true,MessageConst.QUERY_IDENTITY_SUCCESS,pageResult);
        }
        return new Result(false,MessageConst.QUERY_IDENTITY_FAIL);
    }

    @GetMapping("/all")
    public Result all(){
       List roles = roleService.all();
       if (roles!=null){
           return new Result(true,MessageConst.QUERY_IDENTITY_SUCCESS,roles);
       }
       return new Result(false,MessageConst.QUERY_IDENTITY_FAIL);
    }

    @GetMapping("/findByUserRoleId")
    public Result findByUserRoleId(Integer id){
        List list = roleService.findByUserRoleId(id);
        if (list!=null){
            return new Result(true,"查询成功",list);
        }
        return new Result(false,"查询失败");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissions){
        int i = roleService.add(role,permissions);
        if (i==1){
            return new Result(true,MessageConst.ADD_IDENTITY_SUCCESS);
        }
        return new Result(false,MessageConst.ADD_IDENTITY_FAIL);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        if (role!=null){
            return new Result(true,MessageConst.QUERY_IDENTITY_SUCCESS,role);
        }
        return new Result(false,MessageConst.QUERY_IDENTITY_FAIL);
    }

    @GetMapping("/findByRolePermission")
    public Result findByRolePermission(Integer id){
        List list = roleService.findByRolePermission(id);
        if (list!=null){
            return new Result(true,MessageConst.QUERY_IDENTITY_SUCCESS,list);
        }
        return new Result(false,MessageConst.QUERY_IDENTITY_FAIL);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissions){
        int i = roleService.edit(role,permissions);
        if (i==1){
            return new Result(true,MessageConst.EDIT_IDENTITY_SUCCESS);
        }
        return new Result(false,MessageConst.EDIT_IDENTITY_FAIL);
    }

    @PostMapping("/delete")
    public Result delete(Integer id){
        int i  = roleService.delete(id);
        if (i==1){
            return new Result(true,MessageConst.DELETE_IDENTITY_SUCCESS);
        }
        return new Result(false,MessageConst.DELETE_IDENTITY_FAIL);
    }
}
